package com.gw.smscheck.controller.sys;

import com.gw.smscheck.entity.sys.DataDictionary;
import com.gw.smscheck.service.sys.DataDictionaryService;
import com.gw.smscheck.util.ResultObj;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *系统数据字典接口
 *@author lhq
 *
 */
@Controller
@RequestMapping("/dataDictionaryController")
@Api(description="【数据字典接口】")
public class DataDictionaryController {
	
	@Autowired
	private DataDictionaryService dataDictionaryService;
	
	/**
	 * 跳转到查询页面
	 * @return
	 */
	@RequestMapping("/goPage")
	@ApiIgnore
	public String goPage(){
		return "/business/views/datadictionary";
	}
	
	/**
	 * 分页查询
	 * @param dataDictionary
	 * @return
	 */
	@ApiOperation(value="分页查询", notes="分页查询")
	@RequestMapping(value="/pageSelect",method={RequestMethod.POST})
	@ResponseBody
	public String pageSelect(DataDictionary dataDictionary){
		return ResultObj.successResultObj(dataDictionaryService.pageSelect(dataDictionary));
	}

	/*public String pageSelect(DataDictionary dataDictionary){
		PageInfo<DataDictionary> pageInfo = dataDictionaryService.pageSelect(dataDictionary);
		return ResultConversion.ToEasyUIDataGrid(pageInfo).toJSONString();
	}*/
	
	/**
	 * 新增
	 * @param dataDictionary
	 * @return
	 */
	@ApiOperation(value="新增", notes="新增")
	@RequestMapping(value="/add",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String add(DataDictionary dataDictionary,HttpServletRequest req){
        HttpSession session = req.getSession();
        String userName = String.valueOf(session.getAttribute("userName"));
        dataDictionary.setCreateUser(userName);
		dataDictionaryService.add(dataDictionary);
		//自动生成index值
		// 数字转字符串自动补0
		String str = String.format("%04d", dataDictionary.getId());
		System.out.println(str); //0001
		Integer tempId=dataDictionary.getPid();
		if(tempId==0){

			dataDictionary.setIndex(str);
			dataDictionaryService.update(dataDictionary);
		}else{
			//根据父节点id去查询父节点的index
			DataDictionary dto = new DataDictionary();
			dto.setId(tempId);
			String tempIndex  =  dataDictionaryService.getById(dto).getIndex() ;
			//拿到父节点的index做处理
			dataDictionary.setIndex(tempIndex+"-"+str);
			System.out.println(tempIndex+"-"+str);
			dataDictionaryService.update(dataDictionary);
		}

		return ResultObj.successResultObj(dataDictionary);
	}
	
	/**
	 * 通过id获取
	 * @param dataDictionary
	 * @return
	 */
	@ApiOperation(value="通过id获取", notes="通过id获取")
	@RequestMapping(value="/getById",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String getById(DataDictionary dataDictionary){
		return ResultObj.successResultObj(dataDictionaryService.getById(dataDictionary));
	}
	
	/**
	 * 修改
	 * @param dataDictionary
	 * @return
	 */
	@ApiOperation(value="修改", notes="修改")
	@RequestMapping(value="/update",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String update(DataDictionary dataDictionary){
		return ResultObj.successResultObj(dataDictionaryService.update(dataDictionary));
	}
	
	/**
	 * 删除
	 * @param dataDictionary
	 * @return
	 */
	@ApiOperation(value="删除", notes="删除")
	@RequestMapping(value="/del",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String del(DataDictionary dataDictionary){
		dataDictionaryService.del(dataDictionary);
		return ResultObj.successResultObj("删除成功！");
	}
	
	/**
	 * 通过id获取其子项集合
	 * @param dataDictionary
	 * @return
	 */
	/*@ApiOperation(value="通过id获取其子项集合", notes="通过id获取其子项集合")
	@RequestMapping(value="/getChildrenById",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String getChildrenById(DataDictionary dataDictionary){
		return ResultObj.successResultObj(dataDictionaryService.getChildrenById(dataDictionary));
	}*/
	
}
